﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Automobile.service.Model;
using System.Data;
using System.Data.SqlClient;

namespace Automobile.service.DAL
{
    public  class JournalDal
    {
        string str = "Data Source=.;Initial Catalog=qichefuwu;Integrated Security=True";

        /// <summary>
        /// 显示日志信息
        /// </summary>
        /// <returns></returns>
        public List<JournalModel> Journal()
        {
            string sql = "select Journa_Id,Journa_Date,Employee_Id,Journa_Content from  Journal ";
            SqlDataAdapter adapter = new SqlDataAdapter(sql, str);
            DataTable table = new DataTable();
            adapter.Fill(table);
            List<JournalModel> list = new List<JournalModel>();
            foreach (DataRow item in table.Rows)
            {
                JournalModel s = new JournalModel();
                s.Journa_Id = Convert.ToInt32(item["Journa_Id"]);               
                s.Journa_Date = Convert.ToDateTime(item["Journa_Date"]);
                s.Journa_Content = item["Journa_Content"].ToString();
                s.Employee_Id = Convert.ToInt32(item["Employee_Id"]);           
            
                list.Add(s);
            }
            return list;
        }
        /// <summary>
        /// 添加日志信息
        /// </summary>
        /// <returns></returns>
        public int AddJournal(JournalModel s)
        {
            string sql = "insert into Journal(Journa_Date,Employee_Id,Journa_Content) values('" + s.Journa_Date + "','" + s.Employee_Id + "','" + s.Journa_Content + "')";
            SqlConnection conn = new SqlConnection(str);
            conn.Open();
            SqlCommand comm = new SqlCommand(sql, conn);
            int i = comm.ExecuteNonQuery();
            conn.Close();
            return i;
        }
        /// <summary>
        /// 删除日志信息
        /// </summary>
        /// <returns></returns>
        public int DeleJourna(int Journa_Id)
        {
            string sql = "delete from Journal where Journa_Id=" + Journa_Id;
            SqlConnection conn = new SqlConnection(str);
            conn.Open();
            SqlCommand comm = new SqlCommand(sql, conn);
            int i = comm.ExecuteNonQuery();
            conn.Close();
            return i;
        }

        /// <summary>
        /// 取单个日志信息
        /// </summary>
        /// <returns></returns>
        public DataTable GetJourna(string Journa_Id)
        {
            string sql = "select Journa_Id,Journa_Date,Employee_Id,Journa_Content from Journa where Journa_Id=" + Journa_Id;

            SqlConnection conn = new SqlConnection(str);
            conn.Open();//打开数据库

            SqlCommand comm = new SqlCommand(sql, conn);
            DataTable table = new DataTable();
            SqlDataAdapter ada = new SqlDataAdapter(comm);
            ada.Fill(table);
            conn.Close();
            return table;
        }

        /// <summary>
        /// 更新日志信息
        /// </summary>
        /// <param name="stu"></param>
        /// <returns></returns>
        public int UpdateJourna(JournalModel s)
        {
            string sql = "update Journa set Journa_Date='" + s.Journa_Date + "',Employee_Id='" + s.Employee_Id + "',Journa_Content='" + s.Journa_Content + "' where Journa_Id='" + s.Journa_Id + "'";
            SqlConnection conn = new SqlConnection(str);
            conn.Open();//打开数据库

            SqlCommand comm = new SqlCommand(sql, conn);
            //执行命令
            int count = comm.ExecuteNonQuery();
            conn.Close();

            return count;
        }
    }
}
